package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ly.bean.Roles;
import com.ly.mapper.AdminMapper;
import com.ly.mapper.RolesMapper;
import com.ly.service.AdminService;
import com.ly.service.RolesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 李岩
 * @date 2021/12/27-11:12
 * @describe
 */
@Component
@Service(interfaceClass = RolesService.class)
public class RolesServiceImpl implements RolesService {

    private final Logger logger= LoggerFactory.getLogger(RolesServiceImpl.class);
    private RolesServiceImpl(){
        System.out.println("RolesServiceImpl   的service层");
    }
    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    //获取用户角色列表
    @Override
    public List<Roles> selectRoleByaId(Integer aId) throws Exception {
        logger.info("获取用户角色列表 "+aId);
        //如果redis中存在直接返回
        if (redisTemplate.hasKey("roles_"+aId)){
            return (List<Roles>) redisTemplate.boundValueOps("roles_"+aId).get();
        }
        //redis中不存在 就从数据库中查询角色列表放到redis中
        List<Roles> rolesList = rolesMapper.selectRoleByaId(aId);
        redisTemplate.boundValueOps("roles_"+aId).set(rolesList);
        return rolesList;
    }
    //获取所有的角色列表
    @Override
    public List<Roles> selectAllRole() throws Exception {
        logger.info("获取所有的角色列表 ");
        //如果redis中存在直接返回
        if (redisTemplate.hasKey("roles_All")){
            return (List<Roles>) redisTemplate.boundValueOps("roles_All").get();
        }
        //redis中不存在 就从数据库中查询角色列表放到redis中
        List<Roles> rolesList = rolesMapper.selectAllRole();
        redisTemplate.boundValueOps("roles_All").set(rolesList);
        return rolesList;
    }
    //删除角色菜单
    @Override
    public void deleteRolesMenuByRid(Integer roleId) throws Exception {
        rolesMapper.deleteRolesMenuByRid(roleId);
    }
    //添加角色的菜单
    @Override
    public void addRolesMenu(Integer roleId, Integer menuId) throws Exception {
        rolesMapper.addRolesMenu(roleId,menuId);
    }
    /**
     * 删除角色
     */
    @Override
    public void deleteRoleByPrimaryKey(Integer id) throws Exception {
        //因为存在外键关联  所哟要删除角色  需要先根据角色id 删除 roles_admins,roles_menu
        rolesMapper.deleteRoleAdminByPrimaryKey(id);
        rolesMapper.deleteRoleMenuByPrimaryKey(id);
        rolesMapper.deleteRoleByPrimaryKey(id);
        redisTemplate.delete("roles_All");
    }
    /**
     * 添加角色
     */
    @Override
    public void insertRoleSelective(Roles record) throws Exception {
        rolesMapper.insertRoleSelective(record);
        redisTemplate.delete("roles_All");
    }
    /**
     *更改角色
     */
    @Override
    public void updateRoleByPrimaryKeySelective(Roles record) throws Exception {
        rolesMapper.updateRoleByPrimaryKeySelective(record);
        redisTemplate.delete("roles_All");
    }
    /**
     * 根据角色id获取角色信息
     */
    @Override
    public Roles selectRoleByPrimaryKey(Integer id) throws Exception {
        return rolesMapper.selectRoleByPrimaryKey(id);
    }
    //根据roles 删除 roles_admins
    @Override
    public void deleteRoleAdminByPrimaryKey(Integer roles) throws Exception {
        rolesMapper.deleteRoleAdminByPrimaryKey(roles);
    }
    //根据roles 删除 roles_menu
    @Override
    public void deleteRoleMenuByPrimaryKey(Integer roles) throws Exception {
        rolesMapper.deleteRoleMenuByPrimaryKey(roles);
    }

}
